//
//  ViewController.m
//  RollingTitle
//
//  Created by tgs on 2017/3/2.
//  Copyright © 2017年 tgs. All rights reserved.
//

#import "ViewController.h"
#import "LHPageScrollView.h"

#import "AllFindViewController.h"
#import "TutorViewController.h"
#import "NewPViewController.h"
#import "PopularityViewController.h"

#define WS(weakSelf)  __weak __typeof(&*self)weakSelf = self;

@interface ViewController ()<UIScrollViewDelegate,MrLvSegmentViewDelegate>

@property (nonatomic, strong) UIScrollView *headerScroll;
@property (nonatomic, strong) UIScrollView *contentScroll;

@property (nonatomic, strong) NSArray *arrTitle;

@property (nonatomic, strong) LHPageScrollView *lhPageScrollView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
     [self stepUI];
}

- (void)stepUI{
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self.view addSubview:self.lhPageScrollView];
    
    
    NSArray *arrVCName = @[@"AllFindViewController",@"TutorViewController",@"NewPViewController",@"PopularityViewController"];
    
    self.contentScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 74, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-74)];
    self.contentScroll.delegate = self;
    self.contentScroll.pagingEnabled = YES;
    self.contentScroll.showsVerticalScrollIndicator = NO;
    self.contentScroll.showsHorizontalScrollIndicator = NO;
    
    for(int i = 0;i<arrVCName.count;i++)
    {
        UIViewController *tempVC = [[[NSClassFromString(arrVCName[i]) class] alloc] init];
        
        [self addChildViewController:tempVC];

    }
    UIViewController *firstVC = self.childViewControllers[0];
    [self scrollViewDidEndDecelerating:self.contentScroll];
    [self.contentScroll addSubview:firstVC.view];
//    for (NSInteger i=0; i<4; i++) {
//        
//        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(i * [UIScreen mainScreen].bounds.size.width, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 74)];
//        if (i == 0) {
//            view.backgroundColor = [UIColor clearColor];
//            view.userInteractionEnabled = YES;
//       
//        }
//        else if (i == 1){
//            view.backgroundColor = [UIColor redColor];
//        }
//        else if (i == 2){
//            view.backgroundColor = [UIColor greenColor];
//        }
//        else {
//            view.backgroundColor = [UIColor orangeColor];
//        }
//        [self.contentScroll addSubview:view];
//    }

    
    
    self.contentScroll.contentSize = CGSizeMake(arrVCName.count*[UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 74);
    [self.view addSubview:self.contentScroll];
    
}

//- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    if ([scrollView isEqual:self.contentScroll]) {
//        NSInteger current = scrollView.contentOffset.x/[UIScreen mainScreen].bounds.size.width;
//        [self.lhPageScrollView selectIndex:current];
//        
//    }
//}

//减速完成
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //通过偏移量得到 当前在第几页
    NSInteger index = scrollView.contentOffset.x/scrollView.frame.size.width;
    UIViewController *vc = self.childViewControllers[index];
    //判断当前视图 是否加载过
    if(![vc isViewLoaded])
    {
        //如果没有加载过
        vc.view.frame = CGRectMake(index*self.contentScroll.frame.size.width, 0, self.contentScroll.frame.size.width, self.contentScroll.frame.size.height);
      
        [self.contentScroll addSubview:vc.view];
    }
    
    [self.lhPageScrollView selectIndex:index];
    
}

#pragma mark-MrLvSegmentDelegate
- (void)selectedIndex:(NSInteger)index {
    
////    [self.contentScroll setContentOffset:CGPointMake(index*[UIScreen mainScreen].bounds.size.width, 0) animated:YES];

    [UIView animateWithDuration:0.4 animations:^{
        
        [self.contentScroll setContentOffset:CGPointMake((index)*self.contentScroll.frame.size.width, 0)];
    } completion:^(BOOL finished) {
        [self scrollViewDidEndDecelerating:self.contentScroll];
    }];
}

#pragma mark - setter
- (void)setSeletedIndex:(NSInteger)seletedIndex
{
    NSLog(@"第%ld个试图",seletedIndex);
    [self.contentScroll setContentOffset:CGPointMake(seletedIndex *[UIScreen mainScreen].bounds.size.width, 0) animated:YES];
}

- (LHPageScrollView *)lhPageScrollView
{
    if (!_lhPageScrollView) {
        _lhPageScrollView = [[LHPageScrollView alloc] init];
        _lhPageScrollView.delegate = self;
        _lhPageScrollView.backgroundColor = [UIColor whiteColor];
        _lhPageScrollView.frame = CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, 44);
        //        _pagingHeaderView.itemWidth = 80;
        _lhPageScrollView.arrTitiles = @[@"热门", @"发现", @"精选", @"最新"];
    }
    return _lhPageScrollView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
